package com.zpark.it.controller;

import com.zpark.it.param.ProductHotParam;
import com.zpark.it.pojo.Category;
import com.zpark.it.service.CategoryService;
import com.zpark.it.utils.R;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * @Title: CategoryController 类别控制类
 * @Author Charles
 * @Package com.zpark.it.Controller
 * @Date 2023/7/21 14:39
 */
@RestController
@RequestMapping("/category")
public class CategoryController {

    @Autowired
    private CategoryService categoryService;

    @GetMapping("/promo/{categoryName}")
    public R byName(@PathVariable String categoryName){
        if (StringUtils.isEmpty(categoryName)){
            return R.fail("类别名称为空，查询失败");
        }
        return categoryService.byName(categoryName);
    }

    /**
     * 热门商品id查询
     * @param productHotParam
     * @param result
     * @return
     */
    @PostMapping("hots")

    public R hot(@RequestBody @Validated ProductHotParam productHotParam, BindingResult result){
        if (result.hasErrors()){
            return R.fail("类别集合查询失败");
        }
        return categoryService.hotsCategory(productHotParam);
    }


    @GetMapping("list")
    public R list(){
        return categoryService.list();
    }


}
